<?php

namespace app\index\controller;

use app\index\controller\CommonController;
use app\index\model\Artcate;
use app\index\model\Article;

/*
 * 贷款攻略
 */
class ArticleController extends CommonController
{
    public function index()
    {
        //贷款攻略
        $artcateList = Artcate::with(['article' => function($query) {

        }])
            ->field('id, name')
            ->order('id asc')
            ->select();

        //热门文章
        $hotList = Article::where('is_hot', '=', 1)
            ->order('add_time desc')
            ->limit(0, 8)
            ->select();

        return view('index', [
            'menuIndex' => 5,
            'artcateList' => $artcateList,
            'hotList' => $hotList
        ]);
    }

    /*
     * 分类列表
     */
    public function cateList()
    {
        $artcate_id = input('param.id');

        $artcateRow = Artcate::where('id', '=', $artcate_id)->find();

        $articleList = Article::where('artcate_id', '=', $artcate_id)
            ->field('id, title, add_time')
            ->order('add_time desc')
            ->paginate(20);

        return view('catelist', [
            'menuIndex' => 5,
            'artcateRow' => $artcateRow,
            'articleList' => $articleList,
        ]);
    }

    /*
     * 贷款攻略内容
     */
    public function content()
    {
        $id = input('param.id');

        $row = Article::with('artcate')->where('id', '=', $id)->find();

        //相关阅读
        $relateList = Article::where('artcate_id', '=', $row['artcate_id'])
            ->where('id', '<>', $row['id'])
            ->order('add_time desc')
            ->limit(0, 6)
            ->select();

        //上一篇
        $preRow = Article::where('artcate_id', '=', $row['artcate_id'])
            ->where('id', '<', $row['id'])
            ->find();

        //下一篇
        $nextRow = Article::where('artcate_id', '=', $row['artcate_id'])
            ->where('id', '>', $row['id'])
            ->find();

        return view('content', [
            'menuIndex' => 5,
            'row' => $row,
            'relateList' => $relateList,
            'preRow' => $preRow,
            'nextRow' => $nextRow,
        ]);
    }

}